package routes

import (
	_ "bookstore/docs"
	"bookstore/server/controller"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"net/http"
)

// 文档生成
// 1、 go get -u github.com/swaggo/swag/cmd/swag@v1.6.0
//2、../bin/swag init

func ApiRoutes() *gin.Engine {
	// gin 框架web应用 路由
	r := gin.Default()
	// 开启swag接口文档
	// docs.SwaggerInfo.BasePath = ""
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	// 路由
	app := r.Group("app")
	{
		app.GET("user/queryUser", func(c *gin.Context) {
			// 日志打印
			logrus.WithFields(logrus.Fields{
				"method": c.Request.Method,
				"path":   c.Request.URL.Path,
			}).Info("Received request")

			c.JSON(http.StatusOK, gin.H{
				"message": "请求成功",
				"code":    "200",
			})
		})
		// ping 数据是否畅通
		app.GET("ping", func(c *gin.Context) {
			c.JSON(200, "success")
		})
		// 用户数据
		// 用户操作
		app.POST("user/users", controller.QueryUser)

	}
	return r
}
